//
//  PlayerPlrc.m
//  mp3播放器
//
//  Created by Justsoso on 16/4/10.
//  Copyright © 2016年 Justsoso. All rights reserved.
//

#import "PlayerLrc.h"

@implementation PlayerLrc

- (instancetype)init
{
    self = [super init];
    if (self) {
        self.timeArr = [NSMutableArray array];
        self.wordArr = [NSMutableArray array];
    }
    return self;
}

/**
 *  解析歌词
 */
- (void)parselrc
{
    NSString *lrcPath = [[NSBundle mainBundle] pathForResource:self.lrc ofType:nil];
    
    NSString *content = [NSString stringWithContentsOfFile:lrcPath encoding:NSUTF8StringEncoding error:nil];
    
    
    NSArray *array = [content componentsSeparatedByString:@"\n"];
    
    for (int i = 0; i < array.count; i++) {
        
        NSString *lineString = [array objectAtIndex:i];
        
        NSArray *lineArray = [lineString componentsSeparatedByString:@"]"];
        
        if ([lineArray[0] length] == 9) {
        
            NSString *str1 = [lineString substringWithRange:NSMakeRange(3, 1)];
            
            NSString *str2 = [lineString substringWithRange:NSMakeRange(6, 1)];
            
            if ([str1 isEqualToString:@":"] && [str2 isEqualToString:@"."]) {
                
                for (int i = 0; i < lineArray.count - 1; i++) {
                    
                    NSString *lrcString = [lineArray objectAtIndex:lineArray.count - 1];
                    
                    //分割区间求歌词时间
                    NSString *timeStr = [[lineArray objectAtIndex:i] substringWithRange:NSMakeRange(1, 5)];
                    NSArray *array = [timeStr componentsSeparatedByString:@":"];//把时间转换成秒
                    NSUInteger currentTime = [array[0] intValue] * 60 + [array[1] intValue];
                    NSString *str = [NSString stringWithFormat:@"%d",currentTime];
                    
                    [self.timeArr addObject:str];
                    [self.wordArr addObject:lrcString];
                }
            }
        }
    }
}
@end
